package game;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

import javax.swing.ImageIcon;

public class Client extends Thread {

	private Socket socket;
	private BufferedReader in;
	private DataOutputStream out;
	private Queue<String> events;

	public Client(String name, Socket socket, BufferedReader in, DataOutputStream out) {
		super("Client Thread");
		events = new ArrayBlockingQueue<String>(20);
		this.socket = socket;
		this.out = out;
		this.in = in;
	}

	public void run() {
		try {
			boolean connected = true;

			while (connected && Game.getInstance().isVisible()) {

				String event = "";
				String temp;
				while ((temp = events.poll()) != null) {
					event = event + temp + "_";
				}

				if (event.equals(""))
					event = "3#ping";

				event = event + "\n";
				if (!event.equals("3#ping\n")) { // stfu ping messages
					System.out.println(event);
				}
				out.writeBytes(event);

				String buffer = in.readLine();

				if (buffer == null)
					System.out.println("No good!");

				String[] e = buffer.split("_");

				for (String s : e) {
					String[] information = s.split("#");
					if (information[0].equals("3")) {
						if (information[1].equals("points")) { // Point
																// opdatering

							String name = information[2];
							int points = Integer.parseInt(information[3]);
							Player p = Game.getInstance().findPlayer(name);
							p.setPoint(points);
							Game.getInstance().getList().updateScoreOnScreen(p);

						} else if (information[1].equals("move")) { // bevægelses
																	// opdatering

							String name = information[2];
							int x = Integer.parseInt(information[3]);
							int y = Integer.parseInt(information[4]);
							String direction = information[5];
							Player p = Game.getInstance().findPlayer(name);
							Game.getInstance().getG().screen.movePlayerOnScreen(p, x, y, direction);
							p.setXpos(x);
							p.setYpos(y);
							p.setDirection(direction);

						} else if (information[1].equals("add")) { // ny spiller
							System.out.println(information[7]);
							String name = information[2];
							int x = Integer.parseInt(information[3]);
							int y = Integer.parseInt(information[4]);
							String direction = information[5];
							int points = Integer.parseInt(information[6]);
							int color = Integer.parseInt(information[7]);
							Player p = new Player(name);
							p.setColor(color);
							p.setXpos(x);
							p.setYpos(y);
							p.setDirection(direction);
							p.setPoint(points);
							Game.getInstance().players.add(p);
							Game.getInstance().getList().updateScoreOnScreen(p);
							Game.getInstance().getG().screen.movePlayerOnScreen(p, x, y, direction); // TODO
																										// could
																										// be
																										// problematic?
						} else if (information[1].equals("shot")) {
							int posx = Integer.parseInt(information[2]);
							int posy = Integer.parseInt(information[3]);
							int dir = Integer.parseInt(information[4]);
							int tarposx = Integer.parseInt(information[5]);
							int tarposy = Integer.parseInt(information[6]);
							int dir2, i;

							Shot tempShot = new Shot();

							boolean wall = false;

							// facing(0=up,1=right,2=down,3=left)
							if (dir == 0 || dir == 2) {
								tempShot.setXAxis(true);
								dir2 = (dir == 2) ? 1 : -1;
								i = posy + dir2;
								wall = Game.getInstance().getG().getTile(posx, i).equals("w");
							} else {
								tempShot.setXAxis(false);
								dir2 = (dir == 1) ? 1 : -1;
								i = posx + dir2;
								wall = Game.getInstance().getG().getTile(i, posy).equals("w");
							}

							if (tarposx == -1 && !wall) {
								boolean stop = false;
								int modx = (dir == 1) ? 1 : (dir == 3) ? -1 : 0;
								int mody = (dir == 2) ? 1 : (dir == 0) ? -1 : 0;
								int j = 0;
								String peek = "w";
								while (!stop) {
									j++;
									if ((dir == 1 || dir == 3) && (i + dir2 > -1) && posy > -1
																	&& (i + dir2 < 20)) {
										peek = Game.getInstance().getG().getTile(i + dir2, posy);
									} else if ((dir == 0 || dir == 2) && (i + dir2 > -1)
																	&& posx > -1 && (i + dir2 < 20)) {

										peek = Game.getInstance().getG().getTile(posx, i + dir2);
									}
									tempShot.addPos2(i);
									if (peek.equals("e")) {

										if (dir == 1 || dir == 3) {
											Image newIcon = Toolkit.getDefaultToolkit()
																			.createImage("./Image/ildVandret.png");
											Game.getInstance().getG().screen.setIcon(newIcon, i,
																			posy);

										} else {
											Image newIcon = Toolkit.getDefaultToolkit()
																			.createImage("./Image/ildLodret.png");
											Game.getInstance().getG().screen.setIcon(newIcon, posx,
																			i);

										}

										i += dir2;
									} else {
										stop = true;
									}
								}
								if ((i + dir2 > -1) && (i + dir2 < 20)) {
									if (dir == 0) {
										tempShot.addPos2(posy - 1);
										if (posy - 1 > -1) {
											Image newIcon = Toolkit.getDefaultToolkit()
																			.createImage("./Image/ildOp.png");
											Game.getInstance().getG().screen.setIcon(newIcon, posx,
																			posy - 1);
											newIcon = Toolkit.getDefaultToolkit()
																			.createImage("./Image/ildModMurNord.png");
											Game.getInstance().getG().screen.setIcon(newIcon, posx,
																			i);
										}
									} else if (dir == 1) {
										tempShot.addPos2(posx + 1);
										if (posx + 1 < 20) {
											Image newIcon = Toolkit.getDefaultToolkit()
																			.createImage("./Image/ildHoejre.png");
											Game.getInstance().getG().screen.setIcon(newIcon,
																			posx + 1, posy);
											newIcon = Toolkit.getDefaultToolkit()
																			.createImage("./Image/ildModMurOest.png");
											Game.getInstance().getG().screen.setIcon(newIcon, i,
																			posy);
										}
									} else if (dir == 2) {
										tempShot.addPos2(posy + 1);
										if (posy + 1 < 20) {
											Image newIcon = Toolkit.getDefaultToolkit()
																			.createImage("./Image/ildNed.png");
											Game.getInstance().getG().screen.setIcon(newIcon, posx,
																			posy + 1);
											newIcon = Toolkit.getDefaultToolkit()
																			.createImage("./Image/ildModMurSyd.png");
											Game.getInstance().getG().screen.setIcon(newIcon, posx,
																			i);
										}
									} else if (dir == 3) {
										tempShot.addPos2(posx - 1);
										if (posx - 1 > -1) {
											Image newIcon = Toolkit.getDefaultToolkit()
																			.createImage("./Image/ildVenstre.png");
											Game.getInstance().getG().screen.setIcon(newIcon,
																			posx - 1, posy);
											newIcon = Toolkit.getDefaultToolkit()
																			.createImage("./Image/ildModMurVest.png");
											Game.getInstance().getG().screen.setIcon(newIcon, i,
																			posy);
										}
									}
								}
							} else if (!wall) {
								// 0 = up, 1 = right, 2 = down, 3 = left
								int modx = (dir == 1) ? 1 : (dir == 3) ? -1 : 0;
								int mody = (dir == 2) ? 1 : (dir == 0) ? -1 : 0;
								int posdiff = Math.abs((dir == 1 || dir == 3) ? posx - tarposx
																: posy - tarposy);

								for (int j = 2; j < posdiff; j++) {
									if (modx == 0) {
										tempShot.addPos2(posy + j * mody);
										Image newIcon = Toolkit.getDefaultToolkit().createImage(
																		"./Image/ildLodret.png");
										Game.getInstance().getG().screen.setIcon(newIcon, posx,
																		posy + j * mody);
									} else {
										tempShot.addPos2(posx + j * modx);
										Image newIcon = Toolkit.getDefaultToolkit().createImage(
																		"./Image/ildVandret.png");
										Game.getInstance().getG().screen.setIcon(newIcon, posx + j
																		* modx, posy);
									}
								}

								if (dir == 0) {
									tempShot.addPos2(posy - 1);
									Image newIcon = Toolkit.getDefaultToolkit().createImage(
																	"./Image/ildOp.png");
									Game.getInstance().getG().screen.setIcon(newIcon, posx,
																	posy - 1);
								} else if (dir == 1) {
									tempShot.addPos2(posx + 1);
									Image newIcon = Toolkit.getDefaultToolkit().createImage(
																	"./Image/ildHoejre.png");
									Game.getInstance().getG().screen.setIcon(newIcon, posx + 1,
																	posy);
								} else if (dir == 2) {
									tempShot.addPos2(posy + 1);
									Image newIcon = Toolkit.getDefaultToolkit().createImage(
																	"./Image/ildNed.png");
									Game.getInstance().getG().screen.setIcon(newIcon, posx,
																	posy + 1);
								} else if (dir == 3) {
									tempShot.addPos2(posx - 1);
									Image newIcon = Toolkit.getDefaultToolkit().createImage(
																	"./Image/ildVenstre.png");
									Game.getInstance().getG().screen.setIcon(newIcon, posx - 1,
																	posy);
								}

							}

							System.out.println("" + posx + " " + posy + " " + dir + " " + dir2
															+ " " + tempShot);

						}
					}
				}

			}

			out.writeBytes("4#Farvel!\n");
			in.close();
			out.close();
			socket.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void sendMoveRequest(String direction) throws IOException {
		if (events.size() < 20)
			if (!events.offer("3#move#" + direction)) {

			}
	}

	public void sendShootRequest() throws IOException {
		if (events.size() < 20) {
			if (!events.offer("3#shoot")) {

			}
		}

	}
}
